package com.frxuexi.park2301.notice

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.frxuexi.lib_base.entity.NoticeBeanItem
import com.frxuexi.lib_base.net.ApiService

class NoticeDataSource(apiService: ApiService) :PagingSource<Int,NoticeBeanItem>(){
    lateinit var apiService: ApiService
    init {
        this.apiService=apiService
    }
    override fun getRefreshKey(state: PagingState<Int, NoticeBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NoticeBeanItem> {
        try {
            val currentPage=params.key?:0
            val notices = apiService.getNotices(currentPage, 10)
            val nextPage= if(notices.data.size==10) currentPage+1 else null
            return LoadResult.Page(notices.data,null,nextPage)
        }catch (e:Exception){
            return LoadResult.Error(e)
        }
    }
}